<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="门票名称" prop="venuesName">
        <el-input
          v-model="queryParams.venuesName"
          placeholder="请输入门票名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="venuesPhone">
        <el-input
          v-model="queryParams.venuesPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="venuesStatus">
        <el-select v-model="queryParams.venuesStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ticket_venues_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证方式" prop="scenicAuth">
        <el-select v-model="queryParams.scenicAuth" placeholder="请选择认证方式" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_auth"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-hasPermi="['ticket:venues:query']">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ticket:venues:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ticket:venues:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ticket:venues:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ticket:venues:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="venuesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门票名称" align="center" prop="venuesName" />
      <el-table-column label="联系电话" align="center" prop="venuesPhone" />
      <el-table-column label="限购数量" align="center" prop="limitPurchaseNum" />
      <el-table-column label="状态" align="center" prop="venuesStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ticket_venues_status" :value="scope.row.venuesStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="认证方式" align="center" prop="scenicAuth">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_auth" :value="scope.row.scenicAuth"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ticket:venues:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.venuesStatus==1"
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handleUpload(scope.row)"
            v-hasPermi="['ticket:venues:onshelf']"
          >上架</el-button>
          <el-button
            v-if="scope.row.venuesStatus==2"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['ticket:venues:offshelf']"
          >下架</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ticket:venues:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场馆门票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="dialogIsCloseOnModal" :close-on-press-escape="dialogIsCloseOnModal">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- 景区信息 -->
        <el-divider content-position="left">场馆信息</el-divider>
        <el-row>
          <el-col :span="24">
            <el-col :span="11">
              <el-form-item label="场馆" prop="ticOtherBaseId">
                  <el-select
                    v-model="form.ticOtherBaseId"
                    placeholder="请选择场馆"
                    clearable
                    style="width: 240px"
                    @change='changeScenicSelect'
                    :disabled='isEdit'
                  >
                    <el-option
                      v-for="dict in dictScenicList"
                      :key="dict.ticOtherId"
                      :label="dict.name"
                      :value="dict.ticOtherId"
                    />
                  </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <!-- 选择场馆后展示详细信息 -->
          <el-col :span="24" v-if='form.ticOtherBaseId'>
            <el-col :span="11">
              <el-form-item label="星级" prop="starDetail">
                <el-input v-model="dictScenicListSelected.starDetail" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开放时间" >
                <el-input v-if="dictScenicListSelected.openTimeStart" :value="`${dictScenicListSelected.openTimeStart}-${dictScenicListSelected.openTimeEnd}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="景点信息">
                <editor v-model="dictScenicListSelected.introduceDetails" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="23">
              <el-form-item label="游客须知">
                <editor v-model="dictScenicListSelected.touristNotice" :readOnly='false' :min-height="100"/>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="地理位置" >
                <el-input v-model="dictScenicListSelected.addressDetails" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="坐标点" prop="starDetail">
                <el-input v-if="dictScenicListSelected.latitude &&dictScenicListSelected.longitude" :value="`${dictScenicListSelected.latitude}-${dictScenicListSelected.longitude}`" disabled/>
                <el-input v-else value="" disabled/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>

        <!-- 门票信息 -->
        <el-divider content-position="left">门票信息</el-divider>
        <el-row>
          <el-col :span="11">
            <el-form-item label="门票名称" prop="venuesName">
              <el-input v-model="form.venuesName" placeholder="请输入门票名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="venuesPhone">
              <el-input v-model="form.venuesPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="有效时间">
              <el-date-picker
                v-model="effectiveTime"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期" @change='setEffectiveTime($event)'>
              </el-date-picker>
            </el-form-item>
          </el-col> -->
        <!-- <el-col :span="11">
            <el-form-item label="标签" prop="scenicLabel">
              <el-input v-model="form.scenicLabel" placeholder="请输入标签" />
            </el-form-item>
          </el-col> -->
          <el-col :span="11">
            <el-form-item label="限购数量" prop="limitPurchaseNum">
              <el-input-number v-model="form.limitPurchaseNum" placeholder="请输入限购数量" :min="0"  controls-position="right"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="11">
            <el-form-item label="数据状态" prop="scenicStatus">
                <el-select
                  v-model="form.scenicStatus"
                  placeholder="数据状态"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.ticket_scenic_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="认证方式" prop="scenicAuth">
              <el-select
                  v-model="form.scenicAuth"
                  placeholder="认证方式"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.sys_user_auth"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVenues, getVenues, delVenues, addVenues, updateVenues, getListTicOherBase,getListScenic,getOnshelf,getOffshelf} from "@/api/ticket/venues";

export default {
  name: "Venues",
  dicts: ['ticket_venues_status', 'sys_user_auth'],
  data() {
    return {
      // 场馆下拉列表
      dictScenicList:[],
      // 选中场馆
      dictScenicListSelected:'',
      // 判断是否编辑新增
      isEdit:false,
      // 方案下拉列表
      dictStockList:[],
      // 选中方案
      dictStockListSelected:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场馆门票表格数据
      venuesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        venuesName: null,
        venuesPhone: null,
        venuesType: null,
        limitPurchaseNum: null,
        venuesStatus: null,
        ticOtherBaseId: null,
        scenicAuth: null,
        venuesProgrammeId: null,
        ticOtherBaseId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        venuesPhone: [
          {
            pattern: /^((0\d{2,3}-?\d{7,8})|(1[3456789]\d{9}))$/,
            message: "请输入正确的联系电话",
            trigger: "blur"
          },
          {required:true,message:"请输入手机号",trigger:"blur"}
        ],
        ticOtherBaseId:[
          { required: true, message: "请选择场馆",trigger: "change",},
        ],
        venuesName:[
          { required: true, message: "请输入门票名称",trigger: "blur",},
        ],
        limitPurchaseNum:[
          { required: true, message: "请输入限购数量",trigger: "blur",},
        ],
        scenicAuth:[
          { required: true, message: "请选择认证方式",trigger: "change",},
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTicOherBase();
    // this.getStockSelectList();
  },
  methods: {
    // 获取场馆信息
    getTicOherBase(){
      getListTicOherBase().then(res=>{
        this.dictScenicList=res.data
      })
    },
    // 选择场馆
    changeScenicSelect(selected){
      console.log('选择场馆',this.dictScenicList,selected)
      this.form.ticOtherBaseId=selected
      this.dictScenicList.map(item=>{
        if(item.ticOtherId==selected){
          this.dictScenicListSelected=item
        }
      })
    },
    // 获取方案信息
    getStockSelectList(){
      getListScenic().then(res=>{
        this.dictStockList=res.data
      })
    },
    // 选择方案
    changeStockSelect(selected){
      console.log(this.dictStockList,selected)
      if(selected){
        this.dictStockList.map(item=>{
          if(item.venuesProgrammeId==selected){
            this.dictStockListSelected=item
            // 自动带出景点信息
            this.changeScenicSelect(item.ticOtherBaseId)
          }
        })
      }else{
        this.changeScenicSelect('')
      }
    },
    // 上架操作
    handleUpload(row){
      let _this=this
      this.$modal.confirm('是否确认上架该数据项？').then(function() {
        getOnshelf(row.venuesId).then(response => {
          console.log(response)
          _this.$modal.msgSuccess("上架成功");
          _this.getList()
        })
      }).catch(() => {});

    },
    // 下架操作
    handleDownload(row){
      let _this=this
      this.$modal.confirm('是否确认下架该数据项？').then(function() {
        getOffshelf(row.venuesId).then(response => {
          console.log(response)
          _this.$modal.msgSuccess("下架成功");
          _this.getList()
        })
      }).catch(() => {});
    },
    /** 查询场馆门票列表 */
    getList() {
      this.loading = true;
      listVenues(this.queryParams).then(response => {
        this.venuesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        venuesId: null,
        venuesName: null,
        venuesPhone: null,
        venuesType: null,
        limitPurchaseNum: null,
        venuesStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ticOtherBaseId: null,
        scenicAuth: null,
        venuesProgrammeId: null,
        ticOtherBaseId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.venuesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场馆门票";
      this.isEdit=false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit=true
      const venuesId = row.venuesId || this.ids
      getVenues(venuesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改场馆门票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysStoresId=this.dictScenicListSelected.sysStoresId;
          if (this.form.venuesId != null) {
            updateVenues(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.sysStoresId=this.dictScenicListSelected.sysStoresId;
            addVenues(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const venuesIds = row.venuesId || this.ids;
      this.$modal.confirm('是否确认删除场馆门票编号为"' + venuesIds + '"的数据项？').then(function() {
        return delVenues(venuesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ticket/venues/export', {
        ...this.queryParams
      }, `venues_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
